package com.mobilad.dao;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;


import com.mobilad.model.User;
import com.mobilad.model.UserImp;

public class UserDAOImp extends SimpleJdbcDaoSupport implements UserDAO {


/**
 * This method save the user into database.
 * after saving the user it call saveUserAuthority to save the user authority role.
 * role id 1 for admin and 2 for signed user.
*/
	
	public boolean saveUser(User user) {
		
		if(user != null){
				String sql = "INSERT INTO client " +
								"(user_name, address, postalcode, city,mob_nr, pwd, mob_model, mob_model_nr, email_address,enabled)" +
								" VALUES (:userName,:address,:postalcode,:city,:mobNr,:pwd,:mobModel,:mobModelNr,:emailAddress,:enabled)";
				
				
				Long userId = user.getUserId();
				if(userId > 0){
					//do update query
				}else{	
	
					SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
	
					long id = -1;
		            KeyHolder keyholder = new GeneratedKeyHolder();
		            try {
		            	 id = getSimpleJdbcTemplate().getNamedParameterJdbcOperations().update(sql, parameterSource, keyholder, new String[] {"user_id"});
		                     if (id > -1) {
		                             id = keyholder.getKey().longValue();
		                             String authority = user.getUserAuthority();
		                         	saveUserAuthority(id,authority);
		                     }
		             } catch (Exception e) {
		                     return false;
		             }
				}
		}
		return true;
	}
	
	
	public void saveUserAuthority(Long userId, String authority){
		
		String sql = "INSERT INTO user_roles (user_id,authority) VALUES(?,?)";
		List<Object[]> params = new ArrayList<Object[]>();
		params.add(new Object[]{userId,authority} );
		
		 getSimpleJdbcTemplate().batchUpdate(sql, params);      
	}
	
	/**
	 * This method check is mobile number already exist.
	 * return true if mobile number is exist else return false.
	 * @param mob_nr
	 * @return boolean
	 */
	  public  boolean isMobileNr(String mob_nr){
			try{  
		      	String sql ="select mob_nr from  client where mob_nr = ?";
		      	List<UserImp> user = getSimpleJdbcTemplate().query(sql,ParameterizedBeanPropertyRowMapper.newInstance(UserImp.class), mob_nr);
		      	//If mobile number not exist
		      	if(user.size() < 1){
		      		return false;
		      	}else{
		      		return true;
		      	}
			}catch(Exception ex){
				return true;
			}

       }
       
	  
//	  @SuppressWarnings("deprecation")
	public User getUser(String mob, String pwd){
		  User user = new UserImp();
		  String sql = "select * from client where mob_nr=? and pwd=?";
		  Object[] params ={mob,pwd};
		  user = (User)getJdbcTemplate().queryForObject(sql,
					new BeanPropertyRowMapper(UserImp.class),params);
		 
		  return user;
		  
	  }
	  
	  
	/*  public boolean userLogin(String ph, String pwd){
		  try{  
		      	String sql ="select mob_nr from  client where mob_nr = ? and pwd = ?";
		      	Object[] params = {ph,pwd} ;
		      	List<UserImp> user = getSimpleJdbcTemplate().query(sql,ParameterizedBeanPropertyRowMapper.newInstance(UserImp.class),params);
		      	//If user not exist
		      	if(user.size()==0){
		      		return false;
		      	}else{
		      		return true;
		      	}
			}catch(Exception ex){
				return false;
			}
	  }
*/
}
